package com.example.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.example.common.Constants;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.Account;
import com.example.entity.User;
import com.example.exception.CustomException;
import com.example.mapper.UserMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 功能：用户业务层
 * 日期：2024/1/12 15:36
 */
@Service
public class UserService {
    @Resource
    private UserMapper userMapper;

    // 添加用户
    public void addUser(User user) {
        User dbUser = this.getUserByAccount(user.getAccount());
        if (ObjectUtil.isNotNull(dbUser)){
            throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR); //用户已存在，不能重复注册
        }
        if (ObjectUtil.isEmpty(user.getPassword())){
            user.setPassword(Constants.USER_DEFAULT_PASSWORD);   //没有密码，就设置默认密码
        }
        if (ObjectUtil.isNotNull(user.getAccount())){
            user.setUsername(user.getAccount());    //默认的用户名就是账号
        }
        user.setRole(RoleEnum.USER.name());
        userMapper.insert(user);
    }

    // 更新用户
    public void updateUser(User user) {
        if (ObjectUtil.isEmpty(user)) { //空对象不更新
            return;
        }
        userMapper.update(user);
    }

    // 查询用户
    public User getUserByUid(Integer uid) {
        if (ObjectUtil.isEmpty(uid)) {
            return null;
        }
        return userMapper.selectByUid(uid);
    }

    // 根据account查询用户
    public User getUserByAccount(String account) {
        if (ObjectUtil.isEmpty(account)) {
            return null;
        }
        return userMapper.selectByAccount(account);
    }

    // 查询所有用户
    public List<User> getAll(User user) {
        if (ObjectUtil.isEmpty(user)) {
            return null;
        }
        return userMapper.selectAll(user);
    }

    // 分页查询用户
    public PageInfo<User> selectPage(User user, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> users = userMapper.selectAll(user);
        return PageInfo.of(users);
    }

    // 删除用户
    public void deleteUserByUid(Integer uid) {
        if (ObjectUtil.isEmpty(uid)) {
            return;
        }
        userMapper.deleteByUid(uid);
    }

    // 批量删除用户
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids){
            this.deleteUserByUid(id);
        }
    }

    public Account login(Account account) {
        Account dbUser = userMapper.selectByAccount(account.getAccount());
        if (ObjectUtil.isNull(dbUser)) {
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
        if (!account.getPassword().equals(dbUser.getPassword())) {
            throw new CustomException(ResultCodeEnum.USER_ACCOUNT_ERROR);
        }
        // 生成token
        String tokenData = dbUser.getId() + "-" + RoleEnum.USER.name();
        String token = TokenUtils.createToken(tokenData, dbUser.getPassword());
        dbUser.setToken(token);
        return dbUser;
    }

    public void register(Account account) {
        if (ObjectUtil.isNotNull(userMapper.selectByAccount(account.getAccount()))) {
            throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR);
        } else {
            account.setRole(RoleEnum.USER.name());
            User user = new User();
            BeanUtil.copyProperties(account, user);
            this.addUser(user);
        }
    }
}